package name.jiaosha.air.company.service.impl;

import java.util.List;

import name.jiaosha.air.company.dao.IBugDao;
import name.jiaosha.air.company.dao.ICarInfoDao;
import name.jiaosha.air.company.dao.ICompanyDao;
import name.jiaosha.air.company.dao.IDepartmentDao;
import name.jiaosha.air.company.dao.IEmployeeDao;
import name.jiaosha.air.company.dao.IExpenseItemDao;
import name.jiaosha.air.company.dao.IPartnerDao;
import name.jiaosha.air.company.dao.IStorageDao;
import name.jiaosha.air.company.domain.Bug;
import name.jiaosha.air.company.domain.CarInfo;
import name.jiaosha.air.company.domain.Company;
import name.jiaosha.air.company.domain.Department;
import name.jiaosha.air.company.domain.Employee;
import name.jiaosha.air.company.domain.ExpenseItem;
import name.jiaosha.air.company.domain.Partner;
import name.jiaosha.air.company.domain.Storage;
import name.jiaosha.air.company.service.ICompanyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ali8ba.common.PageInfo;
import com.ali8ba.common.PinyinToolkit;

@Service
public class CompanyService implements ICompanyService {

	@Autowired
	private ICompanyDao companyDao;
	@Autowired
	private ICarInfoDao carInfoDao;
	@Autowired
	private IDepartmentDao departmentDao;
	@Autowired
	private IStorageDao storageDao;
	@Autowired
	private IEmployeeDao employeeDao;
	@Autowired
	private IExpenseItemDao expenseItemDao;
	@Autowired
	private IBugDao bugDao;
	@Autowired
	private IPartnerDao partnerDao;

	@Override
	public Company getCompanyById(int id) {

		return companyDao.get(id);
	}

	@Override
	public void createCompany(Company company) {
		companyDao.save(company);
	}

	@Override
	public void createCarInfo(CarInfo carInfo) {
		carInfoDao.save(carInfo);
	}

	@Override
	public CarInfo getCarInfoById(int id) {

		return carInfoDao.get(id);
	}

	@Override
	public PageInfo<CarInfo> getCarListByComId(Integer comId, int index,
			int pageSize) {

		String jpql = "SELECT c FROM CarInfo c";

		return carInfoDao.findForPage(jpql, index, pageSize, null);

	}

	@Override
	public void updateCarInfo(CarInfo carInfo) {

		carInfoDao.saveOrUpdate(carInfo);
	}

	@Override
	public PageInfo<Department> getDepartmentByComId(Integer comId, int index,
			int pageSize) {

		String jpql = "SELECT d FROM Department d";

		return departmentDao.findForPage(jpql, index, pageSize, null);
	}

	@Override
	public Department getDepartmentById(Integer id) {

		return departmentDao.get(id);
	}

	@Override
	public void createDepartment(Department department) {
		department.setPinyin(PinyinToolkit.cn2FirstSpell(department.getName()));
		departmentDao.save(department);

	}

	@Override
	public void updateDepartment(Department department) {
		department.setPinyin(PinyinToolkit.cn2FirstSpell(department.getName()));
		departmentDao.saveOrUpdate(department);

	}

	@Override
	public void createEmployee(Employee employee) {

		employeeDao.save(employee);

	}

	@Override
	public void updateEmployee(Employee employee) {

		employeeDao.saveOrUpdate(employee);

	}

	@Override
	public Employee getEmployeeById(int id) {

		return employeeDao.get(id);
	}

	@Override
	public List<Employee> getEmployeeByComId(Integer comId) {
		String jpql = "SELECT e FROM Employee e WHERE e.company.id=?1";
		return employeeDao.find(jpql, comId);
	}

	@Override
	public PageInfo<Storage> getStorageByComId(Integer comId, int index,
			int pageSize) {

		String jpql = "SELECT s FROM Storage s";
		return storageDao.findForPage(jpql, index, pageSize, null);

	}

	@Override
	public Storage getStorageById(Integer id) {

		return storageDao.get(id);
	}

	@Override
	public void createStorage(Storage storage) {
		
		storage.setPinyin(PinyinToolkit.cn2FirstSpell(storage.getName()));
		storageDao.save(storage);

	}

	@Override
	public void updateStorage(Storage storage) {
		storage.setPinyin(PinyinToolkit.cn2FirstSpell(storage.getName()));
		storageDao.saveOrUpdate(storage);

	}

	@Override
	public ExpenseItem getExpenseItemById(Integer id) {

		return expenseItemDao.get(id);
	}

	@Override
	public void createExpenseItem(ExpenseItem item) {

		item.setPinyin(PinyinToolkit.cn2FirstSpell(item.getName()));
		expenseItemDao.save(item);
	}

	@Override
	public void updateExpenseItem(ExpenseItem item) {
		item.setPinyin(PinyinToolkit.cn2FirstSpell(item.getName()));
		expenseItemDao.saveOrUpdate(item);
	}

	@Override
	public PageInfo<ExpenseItem> getExpenseItemByComId(Integer comId,
			int index, int pageSize) {
		String jpql = "SELECT s FROM ExpenseItem s";
		return expenseItemDao.findForPage(jpql, index, pageSize, null);
	}

	@Override
	public void createBug(Bug bug) {

		bug.setPinyin(PinyinToolkit.cn2FirstSpell(bug.getName()));
		bugDao.save(bug);
	}

	@Override
	public Bug getBugById(Integer id) {

		return bugDao.get(id);
	}

	@Override
	public PageInfo<Bug> getBugListByComId(Integer comId, int index,
			int pageSize) {

		String jpql = "SELECT b FROM Bug b";
		return bugDao.findForPage(jpql, index, pageSize, null);
	}

	@Override
	public void updateBug(Bug bug) {
		bug.setPinyin(PinyinToolkit.cn2FirstSpell(bug.getName()));
		bugDao.saveOrUpdate(bug);

	}

	@Override
	public void createPartner(Partner partner) {
	
		partner.setPinyin(PinyinToolkit.cn2FirstSpell(partner.getCompanyName()));
		partnerDao.save(partner);
		
	}

	@Override
	public Partner getPartnerById(Integer id) {
		
		return partnerDao.get(id);
	}

	@Override
	public PageInfo<Partner> getPartnerListByComId(Integer comId, int index,
			int pageSize) {
		
		String jpql = "SELECT b FROM Partner b";
		return partnerDao.findForPage(jpql, index, pageSize, null);
	}

	@Override
	public void updatePartner(Partner partner) {
		partner.setPinyin(PinyinToolkit.cn2FirstSpell(partner.getCompanyName()));
		partnerDao.saveOrUpdate(partner);
		
	}
	
	

}
